package com.haizhi.crm.biz.dao;

import com.haizhi.crm.biz.model.ConcernedDo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by yangyijun on 2018/5/10.
 */
@Repository
public interface ConcernedDao extends JpaRepository<ConcernedDo, Long> {

    List<ConcernedDo> findByCompanyKey(String companyKey);

    @Query("select c from ConcernedDo c where c.userId =:userId and c.companyKey=:companyKey and c.groupId in :groupIds" )
    List<ConcernedDo> findBy(@Param("companyKey" ) String companyKey, @Param("userId" ) Long userId, @Param("groupIds" ) Long[] groupIds);

    List<ConcernedDo> findByCompanyKeyAndUserId(String companyKey, Long userId);

    /**
     * 根据用户ids查询关注的客户
     *
     * @param userIds
     * @return
     */
    @Query("select c.companyKey from ConcernedDo c where c.userId in :userIds" )
    List<String> findByUserIds(@Param("userIds" ) Long[] userIds);

    /**
     * 根据用户ids查询关注的客户
     *
     * @param userIds
     * @return
     */
    @Query("select c.companyKey from ConcernedDo c where c.userId in :userIds" )
    List<String> findByUserIds(@Param("userIds" ) List<Long> userIds);

    @Query(value = "SELECT DISTINCT(c.company_key) FROM tb_concerned c WHERE length(trim(c.company_key)) > 0 and c.user_id in :userIds", nativeQuery = true)
    List<String> findCompanyKeysByUserIds(@Param("userIds" ) Long[] userIds);
}
